package net.pulseproject.commons.gui;

import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;

import com.google.inject.Inject;

public class IconsCacheImpl implements IconsCache {
	
	private final IconsLoader loader;
	
	private final Map<IconType, ImageIcon> cachedIcons = new HashMap<IconType, ImageIcon>();
	
	
	@Inject public IconsCacheImpl(final IconsLoader loader) {
		this.loader = loader;
	}

	@Override public final ImageIcon load(final IconType type) {
		final ImageIcon storedIcon = this.cachedIcons.get(type);
		if(storedIcon != null) {
			return storedIcon;
		}
		
		final ImageIcon newIcon = this.loader.load(type);
		 this.cachedIcons.put(type, newIcon);
		return newIcon;
	}

}
